﻿package kr.pfg.pfgTracer.view.workspace
{
	import caurina.transitions.Tweener;
	
	import display.Align;
	import display.CTextArea;
	
	import events.ButtonEvent;
	import events.CScrollPaneEvent;
	
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.Timer;
	
	import skinfactory.SkinButton;
	import skinfactory.SkinImage;
	import skinfactory.SkinScaleImage;

	public class VWorkSpace extends Sprite
	{
		//private var tabAll		: VTabAll;
		
		private var textBG: SkinScaleImage;
		
		private var textAreaAll: CTextArea;
		
		private var textAreaMember: Array;
		
		private var btnClipboard: SkinButton;
		private var btnSave		: SkinButton;
		private var msgClipboard: SkinImage;
		private var clipboard: Clipboard;
		
		public var key: String;
		public var fileName: String;
		
		private var btnPadding: int;
		
		public function VWorkSpace()
		{
			this.init();
		}
		private function init(): void
		{
			textAreaMember = new Array();
			
			textBG = new SkinScaleImage( "workspace_bg" );			
			textAreaAll = new CTextArea();
			textAreaAll.addEventListener( CScrollPaneEvent.ACTIVE, onTextAreaScrollStatusListener );
			textAreaAll.addEventListener( CScrollPaneEvent.DEACTIVE, onTextAreaScrollStatusListener );

			btnClipboard = new SkinButton( "workspace_btnClipboard" );
			btnClipboard.addEventListener( ButtonEvent.CLICK, onClipboardClickListener );
			
			btnSave = new SkinButton( "workspace_btnSave" );
			btnSave.addEventListener( ButtonEvent.CLICK, onSaveClickListener );
			
			msgClipboard = new SkinImage( "workspace_msg_Clipboard" );
			msgClipboard.alpha = 0;
			
			clipboard = Clipboard.generalClipboard;
			
			btnPadding = 0;
						
			this.setPosition();
			this.initChild();
		}
		public function addionTextArea (): void
		{
			// 새로운 swf 왔을때마다  textArea를 새로 생성후 배열에 담는다 
			
			//textAreaMember.push( textAreaAll );
		}
		
		private function setPosition(): void
		{			
			textAreaAll.y = 0;
			btnClipboard.x = textAreaAll.width - btnClipboard.width - btnPadding - 5;
			btnClipboard.y = 5;
			
			btnSave.x = btnClipboard.x;
			btnSave.y = Align.getPosY( btnClipboard, 2 );
			
			msgClipboard.x = Align.getCenterX( textAreaAll, msgClipboard );
			msgClipboard.y = Align.getCenterY( textAreaAll, msgClipboard );
		}
		private function initChild(): void
		{
			this.addChild( textAreaAll );			
			this.addChild( btnClipboard );
			this.addChild( btnSave );
			this.addChild( msgClipboard );
		}
		
		override public function set width(value:Number):void
		{			
			textAreaAll.width = value;
			this.setPosition();
		}
		override public function set height(value:Number):void
		{
			textAreaAll.height = value;
			this.setPosition();		
		}
		override public function get width():Number
		{
			return textAreaAll.width;
		}
		
		public function setData ( message: String, type: String ): void
		{
			textAreaAll.text ( message, type );
		}
		
		private function onClipboardClickListener( e: ButtonEvent ): void
		{
			clipboard.clear();
			clipboard.setData( ClipboardFormats.TEXT_FORMAT, textAreaAll.log );
			
			Tweener.addTween( msgClipboard, { alpha: 1, time: 0.5 } );
			var timer: Timer = new Timer( 1000, 1 );
			timer.start();
			timer.addEventListener( TimerEvent.TIMER_COMPLETE, onTimerCompleteListener );
		}
		private function onTimerCompleteListener( e: TimerEvent ): void
		{
			Tweener.addTween( msgClipboard, { alpha: 0, time: 0.5 } );
		}
		private function onSaveClickListener( e: ButtonEvent ): void
		{
			var file: File = File.documentsDirectory.resolvePath( "PFG_"+(fileName.split( "." ))[0]+".txt" );
			try
			{
				file.browseForSave("Save As");
				
				file.addEventListener( Event.SELECT, onSaveDataListener );
			}
			catch ( e: Error )
			{
				trace( "Failed", e.message );
			}
		}
		private function onSaveDataListener( e: Event ): void
		{
			trace(textAreaAll.log);
			var newFile: File = e.target as File;
			var saveData: String = textAreaAll.log;
			var stream: FileStream = new FileStream();
			stream.open( newFile, FileMode.WRITE );
			//stream.writeUTF( saveData );
			stream.writeMultiByte( saveData, "euc-kr" );
			stream.close();
		}
		private function onTextAreaScrollStatusListener( e: CScrollPaneEvent ): void
		{
			trace("Scroll : " + e.type );
			switch( e.type )
			{
				case CScrollPaneEvent.ACTIVE:
					btnPadding = 15;
					break;
				case CScrollPaneEvent.DEACTIVE:
					btnPadding = 0;
					break;
			}
			this.setPosition();
		}
	}
}